﻿using Hx.ADSyncPlatform.ActiveDirectory.Model;
using Hx.ADSyncPlatform.Infrastructure.Page;
using System;
using System.DirectoryServices;
using System.Linq;
using System.Text;
namespace Hx.ADSyncPlatform.ActiveDirectory.Api
{
    public class OrganizationsApi : ApiBase
    {
        public OrganizationsApi(ActiveDirectoryClient activeDirectoryClient) : base(activeDirectoryClient)
        {


        }
        public PageResult<User> PageList(string ou = "", string group = "", string accountName = "", int pageIndex = 1, int pageSize = 10)
        {
            DirectoryEntry de = GetDirectoryObject();

            SearchScope ss = SearchScope.OneLevel;
            StringBuilder sb = new StringBuilder(1024);

            if (string.IsNullOrEmpty(ou))
            {
                ss = SearchScope.Subtree;
                sb.Append("(&(objectCategory=person)(objectClass=user)");
            }
            else
            {
                sb.Append("(&(objectClass=organizationalUnit)");
                de = GetTargetLDAP(ou);
                sb.Append("(ou=" + ou + "*)");

                //(&(objectcategory = organizationalUnit)(cn = " + cn + "))
            }
            sb.Append(")");

            string filter = sb.ToString();

            DirectorySearcher deSearch = new DirectorySearcher(de);
            deSearch.SearchScope = ss;
            deSearch.Filter = filter;
            deSearch.PageSize = 200000;
            deSearch.Sort = new SortOption("DisplayName", SortDirection.Ascending);
            SearchResultCollection src = deSearch.FindAll();
            int total = src.Count;
            int startNum = Math.Max(pageIndex - 1, 0) * pageSize;
            //int endNum= Math.Max(pageIndex, 0) * pageSize;

            var searchResults = src.OfType<SearchResult>().Skip(startNum).Take(pageSize).ToList();

            var userList = searchResults.Select(n =>
              {
                  //return n.Properties?.ConvertTo<User>();
                  var searchDe = n.GetDirectoryEntry();
                  return new User()
                  {
                      cn = searchDe.Properties.Contains("cn") ? searchDe.Properties["cn"].Value.ToString() : "",
                      sAMAccountName = searchDe.Properties.Contains("sAMAccountName") ? searchDe.Properties["sAMAccountName"].Value.ToString() : "",
                      displayname = searchDe.Properties.Contains("displayname") ? searchDe.Properties["displayname"].Value.ToString() : "",
                      department = searchDe.Properties.Contains("department") ? searchDe.Properties["department"].Value.ToString() : "",
                      mail = searchDe.Properties.Contains("mail") ? searchDe.Properties["mail"].Value.ToString() : "",
                      name = searchDe.Properties.Contains("name") ? searchDe.Properties["name"].Value.ToString() : "",
                      mobile = searchDe.Properties.Contains("mobile") ? searchDe.Properties["mobile"].Value.ToString() : "",
                      description = searchDe.Properties.Contains("description") ? searchDe.Properties["description"].Value.ToString() : "",
                      telephoneNumber = searchDe.Properties.Contains("telephoneNumber") ? searchDe.Properties["telephoneNumber"].Value.ToString() : "",
                      co = searchDe.Properties.Contains("co") ? searchDe.Properties["co"].Value.ToString() : "",
                      postalAddress = searchDe.Properties.Contains("postalAddress") ? searchDe.Properties["postalAddress"].Value.ToString() : "",
                      postalCode = searchDe.Properties.Contains("postalCode") ? searchDe.Properties["postalCode"].Value.ToString() : "",
                      Workstations = searchDe.Properties.Contains("cn") ? searchDe.Properties["cn"].Value.ToString() : "",

                  };
              }).ToList();
            PageResult<User> pageResult = new PageResult<User>(userList, pageIndex, total);
            return pageResult;


        }
    }
}
